// pages/travel/list.js
import { getUserFavoritePlaceListForPageForUser,//获取用户收藏景点列表
  getUserFavoriteItineraryListForPageForUser,//获取用户收藏行程列表
  getUserFavoriteListAllForUser,//收藏分类列表
 } from '../../api/home'
import { imageURL } from '../../../utils/request'

Page({
  data: {
    imageURL:'',
    activeTab: 0,
    tabs: [],
    activeIndex: 0,
    scrollLeft: 0,
    user_favorite_id:'',
    playList:[],
    keyword:'',
    keywordPlace:'',
  },
  onLoad(){
    this.getCollectType()
    this.getCollectPlay()
    this.getCollectPlace()
    this.setData({
      imageURL
    })
  },
  async getCollectType(){
    const res = await getUserFavoriteListAllForUser({})
    const list = [{
      user_favorite_id:'',
      name:'全部',
    }]
    this.setData({
      tabs:[...list, ...(res.data.list || [])]
    })
  },
  async getCollectPlay(){
    const res = await getUserFavoritePlaceListForPageForUser({
      page:1,
      rows:9999,
      keyword:this.data.keyword,
      user_favorite_id:this.data.user_favorite_id
    })
    
    this.setData({
      playList:res.data.list
    })
  },
  async getCollectPlace(){
    const res = await getUserFavoriteItineraryListForPageForUser({
      page:1,
      rows:9999,
      keyword:this.data.keywordPlace,
    })
    this.setData({
      placeList:res.data.list
    })
  },
  // 點擊切換Tab
  scrollTab(e) {
    const index = e.currentTarget.dataset.index;
    const user_favorite_id = e.currentTarget.dataset.user_favorite_id;
    
    this.setData({
      activeIndex: index,
      user_favorite_id:user_favorite_id,
      scrollLeft: (index - 1) * 60 // 滾動到合適位置
    });
    this.getCollectPlay()
  },
// 輸入事件
onInput(e) {
  this.setData({
    keyword: e.detail.value
  })
  if(e.detail.value==''){
    this.getCollectPlay()
  }
},
onInput2(e) {
  this.setData({
    keywordPlace: e.detail.value
  })
  if(e.detail.value==''){
    this.getCollectPlace()
  }
},
async onSearch() {
  if (!this.data.keyword.trim()) {
    wx.showToast({
      title: '請輸入搜索內容',
      icon: 'none'
    });
    return;
  }
  this.getCollectPlay()
},
  async onSearchPlace() {
    if (!this.data.keywordPlace.trim()) {
      wx.showToast({
        title: '請輸入搜索內容',
        icon: 'none'
      });
      return;
    }
    this.getCollectPlace()
  },

  // 滑動切換Tab
  swiperChange(e) {
    const index = e.detail.current;
    this.setData({
      activeIndex: index,
      scrollLeft: (index - 1) * 60
    });
  },
  switchTab(e) {
    console.log(e.currentTarget.dataset.index);
    this.setData({
      activeTab: e.currentTarget.dataset.index
    })
  },
  goDetails(e){
    const place_id = e.currentTarget.dataset.place_id
    wx.navigateTo({
      url: `/pages/Itinerary/SceneryDetails/SceneryDetails?place_id=${place_id}`,
    })
  },
  goItineraryDetails(e){
    const itinerary_id = e.currentTarget.dataset.itinerary_id
    wx.navigateTo({
      url: `/pages/Itinerary/ItineraryDetails/ItineraryDetails?itinerary_id=${itinerary_id}`,
    })
  },
  
})